<?php
// +-----------------+---------------------+------+-----+---------+----------------+
// | Field           | Type                | Null | Key | Default | Extra          |
// +-----------------+---------------------+------+-----+---------+----------------+
// | id              | int(11)             | NO   | PRI | NULL    | auto_increment |
// | member_id       | int(11) unsigned    | YES  |     | 0       |                |
// | member_name     | varchar(30)         | YES  |     |         |                |
// | request_method  | varchar(30)         | YES  |     |         |                |
// | controller_name | varchar(30)         | YES  |     |         |                |
// | action_name     | varchar(30)         | YES  |     |         |                |
// | param           | text                | YES  |     | NULL    |                |
// | query_string    | varchar(255)        | YES  |     |         |                |
// | user_agent      | varchar(255)        | YES  |     |         |                |
// | request_ip      | bigint(20) unsigned | YES  |     | 0       |                |
// | create_time     | int(11) unsigned    | YES  |     | 0       |                |
// +-----------------+---------------------+------+-----+---------+----------------+
namespace app\admin\model;

use think\Model;
class AdminLog extends Model
{
    protected $updateTime = null;

    /*
    记录管理员操作日志
    */
    public function add(){
        $data = [];
        $req = request();
        $ctrl_name = $req->controller();
        $ctrl_name = strtolower($ctrl_name);
        if(in_array($ctrl_name, array('index'))){
            return true;
        }
        $data['controller_name'] = $ctrl_name;

        $act_name = $req->action();
        $act_name = strtolower($act_name);
        if(strpos($act_name, 'ajax_') === 0){
            return true;
        }
        $data['action_name'] = $act_name;

        $member = login_member();
        if(empty($member)){
            $data['member_id'] = 0;
            $data['member_name'] = "";
        }else{
            $data['member_id'] = $member["id"];
            $data['member_name'] = substr($member["name"], 0, 30);
        }

        $data['request_method'] = $req->method();
        $param = $req->param();
        if(empty($param)){
            $param = [];
        }
        if(isset($param['content']) && count($param['content']) > 1000){
            unset($param['content']);
        }
        $param = json_encode($param);
        $data['param'] = $param;

        $query_str = $req->query();
        if(empty($query_str)){
            $query_str = "";
        }
        $data['query_string'] = $query_str;

        $user_agent = $req->header('user-agent');
        $data['user_agent'] = substr($user_agent, 0, 200);
        $data['request_ip'] = ip2long($req->ip());
        $this->data($data)->allowField(true)->save();
        return $this->id;
    }
}
